package tgt

import (
	"fmt"
	log "github.com/sirupsen/logrus"
	"vmService/vmservice/db"
)

func insertGatewayData(gwInfo GatewayInfo) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	result, err := dbClient.DB.Exec("insert into vmservice.tgt_gateway values (?,?,?,?,?)", 1, gwInfo.Name, gwInfo.Host, gwInfo.CreateTime, gwInfo.Active)
	if err != nil {
		return -1, err
	}
	id, _ := result.LastInsertId()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return id, nil
}

func deleteGatewayData(host string) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	log.Debugf("deleteGatewayData: sql: %s", fmt.Sprintf("delete from vmservice.tgt_gateway where host='%s'", host))
	result, err := dbClient.DB.Exec(fmt.Sprintf("delete from vmservice.tgt_gateway where host='%s'", host))
	if err != nil {
		return -1, err
	}
	num, _ := result.RowsAffected()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return num, nil
}

func updateGatewayInfo(host string, active int) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("update vmservice.tgt_gateway set active=%d where host='%s'", active, host)
	log.Debugf("updateGatewayInfo: sql: %s", sqlStr)
	result, err := dbClient.DB.Exec(sqlStr)
	if err != nil {
		return -1, err
	}
	num, _ := result.RowsAffected()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return num, nil
}

func queryGatewayInfo(host string) (GatewayInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return GatewayInfo{}, err
	}
	defer dbClient.CloseDB()
	log.Debugf("queryGatewayInfo: sql: %s", fmt.Sprintf("select * from vmservice.tgt_gateway where host='%s'", host))
	row := dbClient.DB.QueryRow(fmt.Sprintf("select * from vmservice.tgt_gateway where host='%s'", host))
	gwInfo := GatewayInfo{}
	id := -1
	if err := row.Scan(&id, &gwInfo.Name, &gwInfo.Host, &gwInfo.CreateTime, &gwInfo.Active); err != nil {
		return GatewayInfo{}, err
	}
	return gwInfo, nil
}

func queryGatewayList() ([]GatewayInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []GatewayInfo{}, err
	}
	defer dbClient.CloseDB()
	log.Debugf("queryGatewayInfo: sql: %s", "select * from vmservice.tgt_gateway")
	rows, err := dbClient.DB.Query("select * from vmservice.tgt_gateway")
	if err != nil {
		return []GatewayInfo{}, err
	}
	gwSlice := make([]GatewayInfo, 0)
	for rows.Next() {
		id := -1
		gwInfo := GatewayInfo{}
		err := rows.Scan(&id, &gwInfo.Name, &gwInfo.Host, &gwInfo.CreateTime, &gwInfo.Active)
		if err != nil {
			return []GatewayInfo{}, err
		}
		gwSlice = append(gwSlice, gwInfo)
	}
	return gwSlice, nil
}

func queryGatewayListByFilter(host string) ([]GatewayInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []GatewayInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("select * from vmservice.tgt_gateway where host='%s'", host)
	log.Debugf("queryGatewayListByFilter: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []GatewayInfo{}, err
	}
	gwSlice := make([]GatewayInfo, 0)
	for rows.Next() {
		id := -1
		gwInfo := GatewayInfo{}
		err := rows.Scan(&id, &gwInfo.Name, &gwInfo.Host, &gwInfo.CreateTime, &gwInfo.Active)
		if err != nil {
			return []GatewayInfo{}, err
		}
		gwSlice = append(gwSlice, gwInfo)
	}
	return gwSlice, nil
}

func insertTargetData(targetInfo TargetDBInfo) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	result, err := dbClient.DB.Exec("insert into vmservice.tgt_target values (?,?,?,?)", 0, targetInfo.Host, targetInfo.TargetID, targetInfo.CreateTime)
	if err != nil {
		return -1, err
	}
	id, _ := result.LastInsertId()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return id, nil
}
func deleteTargetData(host, targetID string) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	sqlStr := ""
	if targetID == "" {
		sqlStr = fmt.Sprintf("delete from vmservice.tgt_target where host='%s'", host)
	} else {
		sqlStr = fmt.Sprintf("delete from vmservice.tgt_target where host='%s' and target_id=%s", host, targetID)
	}
	log.Debugf("deleteTargetData: sql: %s", sqlStr)
	result, err := dbClient.DB.Exec(sqlStr)
	if err != nil {
		return -1, err
	}
	num, _ := result.RowsAffected()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return num, nil
}

func queryTargetList(host string) ([]TargetDBInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []TargetDBInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := ""
	if host == "" {
		sqlStr = "select * from vmservice.tgt_target"
	} else {
		sqlStr = fmt.Sprintf("select * from vmservice.tgt_target where host='%s'", host)
	}
	log.Debugf("queryTargetList: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []TargetDBInfo{}, err
	}
	targetSlice := make([]TargetDBInfo, 0)
	for rows.Next() {
		id := -1
		targetInfo := TargetDBInfo{}
		err := rows.Scan(&id, &targetInfo.Host, &targetInfo.TargetID, &targetInfo.CreateTime)
		if err != nil {
			return []TargetDBInfo{}, err
		}
		targetSlice = append(targetSlice, targetInfo)
	}
	return targetSlice, nil
}

func queryTargetListByFilter(host, targetID string) ([]TargetDBInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []TargetDBInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("select * from vmservice.tgt_target where host='%s' and target_id=%s", host, targetID)
	log.Debugf("queryTargetListByFilter: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []TargetDBInfo{}, err
	}
	targetSlice := make([]TargetDBInfo, 0)
	for rows.Next() {
		id := -1
		targetInfo := TargetDBInfo{}
		err := rows.Scan(&id, &targetInfo.Host, &targetInfo.TargetID, &targetInfo.CreateTime)
		if err != nil {
			return []TargetDBInfo{}, err
		}
		targetSlice = append(targetSlice, targetInfo)
	}
	return targetSlice, nil
}

func insertLUNData(lunInfo LUNDBInfo) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	result, err := dbClient.DB.Exec("insert into vmservice.tgt_lun values (?,?,?,?,?,?,?)", 0, lunInfo.Host, lunInfo.TargetID, lunInfo.LunID, lunInfo.LogicalID, lunInfo.LunSize, lunInfo.CreateTime)
	if err != nil {
		return -1, err
	}
	id, _ := result.LastInsertId()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return id, nil
}

func deleteLUNData(host, targetID string) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("delete from vmservice.tgt_lun where host='%s' and target_id=%s", host, targetID)
	log.Debugf("deleteLUNData: sql: %s", sqlStr)
	result, err := dbClient.DB.Exec(sqlStr)
	if err != nil {
		return -1, err
	}
	num, _ := result.RowsAffected()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return num, nil
}

func queryTargetLUNList(host, targetID string) ([]LUNDBInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []LUNDBInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := ""
	if host == "" {
		sqlStr = "select * from vmservice.tgt_lun"
	} else if host != "" && targetID == "" {
		sqlStr = fmt.Sprintf("select * from vmservice.tgt_lun where host='%s'", host)
	} else {
		sqlStr = fmt.Sprintf("select * from vmservice.tgt_lun where host='%s' and target_id=%s", host, targetID)
	}
	log.Debugf("queryTargetLUNList: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []LUNDBInfo{}, err
	}
	lunSlice := make([]LUNDBInfo, 0)
	for rows.Next() {
		id := -1
		lunInfo := LUNDBInfo{}
		err := rows.Scan(&id, &lunInfo.Host, &lunInfo.TargetID, &lunInfo.LunID, &lunInfo.LogicalID, &lunInfo.LunSize, &lunInfo.CreateTime)
		if err != nil {
			return []LUNDBInfo{}, err
		}
		lunSlice = append(lunSlice, lunInfo)
	}
	return lunSlice, nil
}

func queryTargetLUNListByFilter(host, targetID, lunID string) ([]LUNDBInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []LUNDBInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("select * from vmservice.tgt_lun where host='%s' and target_id=%s and lun_id=%s", host, targetID, lunID)
	log.Debugf("queryTargetLUNListByFilter: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []LUNDBInfo{}, err
	}
	lunSlice := make([]LUNDBInfo, 0)
	for rows.Next() {
		id := -1
		lunInfo := LUNDBInfo{}
		err := rows.Scan(&id, &lunInfo.Host, &lunInfo.TargetID, &lunInfo.LunID, &lunInfo.LogicalID, &lunInfo.LunSize, &lunInfo.CreateTime)
		if err != nil {
			return []LUNDBInfo{}, err
		}
		lunSlice = append(lunSlice, lunInfo)
	}
	return lunSlice, nil
}

func insertTargetIPData(targetIPInfo TargetIPDBInfo) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	result, err := dbClient.DB.Exec("insert into vmservice.tgt_ip values (?,?,?,?,?)", 0, targetIPInfo.Host, targetIPInfo.TargetID, targetIPInfo.IPAddress, targetIPInfo.CreateTime)
	if err != nil {
		return -1, err
	}
	id, _ := result.LastInsertId()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return id, nil
}

func deleteTargetIPData(host, targetID string) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("delete from vmservice.tgt_ip where host='%s' and target_id=%s", host, targetID)
	log.Debugf("deleteTargetIPData: sql: %s", sqlStr)
	result, err := dbClient.DB.Exec(sqlStr)
	if err != nil {
		return -1, err
	}
	num, _ := result.RowsAffected()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return num, nil
}

func queryTargetIPList(host, targetID string) ([]TargetIPDBInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []TargetIPDBInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := ""
	if host == "" {
		sqlStr = "select * from vmservice.tgt_ip"
	} else if host != "" && targetID == "" {
		sqlStr = fmt.Sprintf("select * from vmservice.tgt_ip where host='%s'", host)
	} else {
		sqlStr = fmt.Sprintf("select * from vmservice.tgt_ip where host='%s' and target_id=%s", host, targetID)
	}
	log.Debugf("queryTargetIPList: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []TargetIPDBInfo{}, err
	}
	targetIPSlice := make([]TargetIPDBInfo, 0)
	for rows.Next() {
		id := -1
		targetIPInfo := TargetIPDBInfo{}
		err := rows.Scan(&id, &targetIPInfo.Host, &targetIPInfo.TargetID, &targetIPInfo.IPAddress, &targetIPInfo.CreateTime)
		if err != nil {
			return []TargetIPDBInfo{}, err
		}
		targetIPSlice = append(targetIPSlice, targetIPInfo)
	}
	return targetIPSlice, nil
}

func queryTargetIPListByFilter(host, targetID, ipAddress string) ([]TargetIPDBInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []TargetIPDBInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("select * from vmservice.tgt_ip where host='%s' and target_id=%s and ip='%s'", host, targetID, ipAddress)
	log.Debugf("queryTargetIPListByFilter: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []TargetIPDBInfo{}, err
	}
	targetIPSlice := make([]TargetIPDBInfo, 0)
	for rows.Next() {
		id := -1
		targetIPInfo := TargetIPDBInfo{}
		err := rows.Scan(&id, &targetIPInfo.Host, &targetIPInfo.TargetID, &targetIPInfo.IPAddress, &targetIPInfo.CreateTime)
		if err != nil {
			return []TargetIPDBInfo{}, err
		}
		targetIPSlice = append(targetIPSlice, targetIPInfo)
	}
	return targetIPSlice, nil
}

func insertTargetUserData(targetUserInfo TargetUserDBInfo) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	result, err := dbClient.DB.Exec("insert into vmservice.tgt_user values (?,?,?,?,?,?)", 0, targetUserInfo.Host, targetUserInfo.TargetID, targetUserInfo.Username, targetUserInfo.Password, targetUserInfo.CreateTime)
	if err != nil {
		return -1, err
	}
	id, _ := result.LastInsertId()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return id, nil
}

func deleteTargetUserData(host, targetID string) (int64, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return -1, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("delete from vmservice.tgt_user where host='%s' and target_id=%s", host, targetID)
	log.Debugf("deleteTargetIPData: sql: %s", sqlStr)
	result, err := dbClient.DB.Exec(sqlStr)
	if err != nil {
		return -1, err
	}
	num, _ := result.RowsAffected()
	//fmt.Printf("insert success, last id:[%d]\n", id)
	return num, nil
}

func queryTargetUserList(host, targetID string) ([]TargetUserDBInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []TargetUserDBInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := ""
	if host == "" {
		sqlStr = "select * from vmservice.tgt_user"
	} else if host != "" && targetID == "" {
		sqlStr = fmt.Sprintf("select * from vmservice.tgt_user where host='%s'", host)
	} else {
		sqlStr = fmt.Sprintf("select * from vmservice.tgt_user where host='%s' and target_id=%s", host, targetID)
	}
	log.Debugf("queryTargetUserList: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []TargetUserDBInfo{}, err
	}
	targetUserSlice := make([]TargetUserDBInfo, 0)
	for rows.Next() {
		id := -1
		targetUserInfo := TargetUserDBInfo{}
		err := rows.Scan(&id, &targetUserInfo.Host, &targetUserInfo.TargetID, &targetUserInfo.Username, &targetUserInfo.Password, &targetUserInfo.CreateTime)
		if err != nil {
			return []TargetUserDBInfo{}, err
		}
		targetUserSlice = append(targetUserSlice, targetUserInfo)
	}
	return targetUserSlice, nil
}

func queryTargetUserListByFilter(host, targetID, username string) ([]TargetUserDBInfo, error) {
	dbClient := db.DBClient{}
	dbClient.InitDBInfo("root", "wfdz@admin2022", "192.168.0.101:32306", "vmservice")
	if err := dbClient.InitDB(); err != nil {
		return []TargetUserDBInfo{}, err
	}
	defer dbClient.CloseDB()
	sqlStr := fmt.Sprintf("select * from vmservice.tgt_user where host='%s' and target_id=%s and username='%s'", host, targetID, username)
	log.Debugf("queryTargetUserListByFilter: sql: %s", sqlStr)
	rows, err := dbClient.DB.Query(sqlStr)
	if err != nil {
		return []TargetUserDBInfo{}, err
	}
	targetUserSlice := make([]TargetUserDBInfo, 0)
	for rows.Next() {
		id := -1
		targetUserInfo := TargetUserDBInfo{}
		err := rows.Scan(&id, &targetUserInfo.Host, &targetUserInfo.TargetID, &targetUserInfo.Username, &targetUserInfo.Password, &targetUserInfo.CreateTime)
		if err != nil {
			return []TargetUserDBInfo{}, err
		}
		targetUserSlice = append(targetUserSlice, targetUserInfo)
	}
	return targetUserSlice, nil
}
